package com.ruike.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.ruike.pojo.Classroom;
import com.ruike.service.AcademicBuildingService;
import com.ruike.service.ClassroomService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 吴泽胜
 * @FileName ClassroomController
 * @date 2021-01-25 11:56 下午
 * @Software: IntelliJ IDEA
 */
@Controller
@RequestMapping("/classroom")
public class ClassroomController {

    @Autowired
    private ClassroomService classroomService;
    @Autowired
    private AcademicBuildingService academicBuildingService;

    /**
     * 获取教室列表
     * @return
     * @throws Exception
     */
    @RequiresRoles(value={"Admin","Super"})
    @ApiOperation(value = "获取教室列表",notes = "获取教室列表的接口")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public String getClassroomList(  @RequestParam(defaultValue = "1")Integer pageNum,
                                     @RequestParam(defaultValue="10")Integer pageSize,
                                     @RequestParam(defaultValue="1")String academic_building_ID,
                                     @RequestParam(defaultValue="")String searchParam,
                                     Model model ) throws Exception {
        List<Classroom> classroomList = searchParam.isEmpty() ?
                classroomService.fuzzyAndAB_IDClassroomList(searchParam, academic_building_ID, pageNum, pageSize) :
                classroomService.fuzzyAndAB_IDClassroomList(searchParam, "", pageNum, pageSize);
        //查询中最好只有一次查询，不要进行多次查询，pageHelper 默认操作第一次查询的结果集
        PageInfo<Classroom> info = new PageInfo<Classroom>(classroomList);
        //将List转换为pageInfo对象，这样可以更方便的在页面中进行分页操作。
        model.addAttribute("pageInfo",info);
        // 获取教学楼的名称
        model.addAttribute("academic_buildingList", academicBuildingService.getAcademicBuildingList());
        // 返回当前搜索的 academic_building_ID
        model.addAttribute("academic_building_ID",academic_building_ID) ;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", classroomList);
        model.addAttribute("Classrooms",jsonObject);
        return "view/classroom";
    }


    /**
     * 更新教室的使用状态
     * @param id
     * @return
     * @throws Exception
     */
    @RequiresRoles(value={"Admin","Super"})
    @ApiOperation(value = "更新教室的使用状态",notes = "更新教室的使用状态的接口")
    @RequestMapping(value = "/updataState/{id}", method = RequestMethod.GET)
    public String updateStateById(@PathVariable("id") Integer id) throws Exception {
        Classroom classroom = classroomService.findClassroomById(id);
        classroom.setJszt( classroom.getJszt().equals("启用") ? "禁用" : "启用");
        classroomService.updataStateById(classroom);
        return "redirect:/classroom";
    }

    /**
     * 通过id删除
     * @param id
     * @throws Exception
     */
    @RequiresRoles(value={"Admin","Super"})
    @ApiOperation(value = "获取分页",notes = "获取分页的接口")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public String deleteClassroomById(@PathVariable Integer id) throws Exception {
        System.out.println( id );
        classroomService.deleteClassroomById(id);
        return "redirect:/classroom";
    }

    /**
     * 更新教室
     * @param classroom
     * @throws Exception
     */
    @RequiresRoles(value={"Admin","Super"})
    @ApiOperation(value = "更新教室",notes = "更新教室的接口")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String updateClassroomById(Classroom classroom) throws Exception {
        classroomService.updateClassroomById(classroom);
        return "redirect:/classroom";
    }

    /**
     * 增加教室
     * @param classroom
     * @throws Exception
     */
    @RequiresRoles(value={"Admin","Super"})
    @ApiOperation(value = "增加教室",notes = "增加教室的接口")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String addClassroom( Classroom classroom) throws Exception {
        System.out.println( classroom );
        classroomService.addClassroom(classroom);
        return "redirect:/classroom";
    }
}
